using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace  TowerDefence
{
    public class EnemyHpItem : MonoBehaviour
    {
        private Enemy mTarget;

        private Scrollbar mHpBar;

       
        public void InitData(Enemy enemy)
        {
            mTarget = enemy;
            mHpBar = transform.Find("HPBar").GetComponent<Scrollbar>();
            enemy.EnemyHpItem = this;
            RefeshHp();
            
        }

        private void LateUpdate()
        {
            if (mTarget == null)
            {
                Destroy(this.gameObject);
            }
            else
            {
                RefershPos();
            }
         
        }

        public void RefershPos()
        {
            Vector3 pos = mTarget.mHpPos.transform.position;
            
            pos=Camera.main.WorldToScreenPoint(pos);

            pos.x -= Screen.width >> 1;
            pos.y -= Screen.height >> 1;

            pos = pos * 1080 / Screen.height;
            pos.y += 15;
            transform.localPosition = pos;

        }

        public void RefeshHp()
        {
            mHpBar.size = mTarget.mHP * 1f / mTarget.mMaxHP;
        }
    }
}

